﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace TransactionTest
{
    class Program
    {
        const string Waiting = "Waiting";
        const string InProgress = "InProgress";

        static void Main(string[] args)
        {
            Task task;
            var machineName = args[0];

            Console.WriteLine(machineName);

            Console.WriteLine("Entering transaction...");
            Console.ReadLine();

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            using (var context = new Model1Container())
            {
                context.Connection.Open();

                Console.WriteLine("Searching for tasks...");
                Console.ReadLine();
                task = context.Tasks.Single(t => t.Status == Waiting);

                Console.WriteLine("Updating task...");
                Console.ReadLine();
                task.Status = InProgress;

                Console.WriteLine("Saving changes...");
                Console.ReadLine();
                context.SaveChanges();

                scope.Complete();
            }

            Console.WriteLine("Executing {0} on {1}", task.Name, machineName);
        }
    }
}
